<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.mysql.fabric.xmlrpc.base.Data" %>
<%@ page import="java.util.Date" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>借出</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/tips.js"></script>
   <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/mFrame/newtool.js"></script>

    <style>

        .toast-center-center {
            top: 34%;
            left: 50%;
            margin-top: -30px;
            margin-left: -150px;
        }


    </style>
</head>
<body>
<%--格式化时间--%>
<%
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String tDate = sdf.format(new Date());

%>



<div class="container shadow-sm p-3 mb-5 bg-white rounded">
    <div class="row " >
        <div class="col-12">
            <div id="reform" >
                <nav class="navbar  navbar-light rounded " style="background-color: #1e5379; margin-bottom: 30px;">
                    <a class="navbar-brand" href="#" style="color: white;">借出登记</a>
                </nav>

            </div>
        </div>

    </div>
    <div class="row">
        <div class="col-12 col-md-2"></div>
        <div class="col-12 col-md-8">
            <form id="myForm">
                <div class="form-group row">
                    <label for="eName" class="col-lg-2 col-form-label">设备名称：</label>
                    <div class="col-lg-10">
                        <input type="text"  id="eName" class="form-control" >
                    </div>
                </div>

                <div class="form-group row">
                    <label for="etype" class="col-lg-2 col-form-label">设备类型</label>
                    <div class="col-lg-10">
                        <select id="etype" class="form-control">
                            <c:forEach var="types" items="${etype}">
                                <option value="${types.id}">${types.type}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="form-group row">
                    <label for="lendName" class="col-lg-2 col-form-label">借用人：&nbsp;&nbsp;&nbsp;&nbsp;</label>
                    <div class="col-lg-10">
                        <input type="text"  id="lendName"  class="form-control" >
                    </div>
                </div>

                <div class="form-group row">
                    <label for="dp" class="col-lg-2 col-form-label">借用部门：</label>
                    <div class="col-lg-10">
                        <select id="dp" class="form-control">
                            <c:forEach var="dp" items="${depar}">
                                <option value="${dp.id}">${dp.lendDepar}</option>
                            </c:forEach>

                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label for="number"    class="col-lg-2 col-form-label">设备数量：</label>
                    <div class="col-lg-10">
                        <input type="text" id="number" class="form-control" >
                    </div>
                </div>
                <div class="form-group row">
                    <label for="agent"   class="col-lg-2 col-form-label">经办人：&nbsp;&nbsp;&nbsp;&nbsp;</label>
                    <div class="col-lg-10">
                        <input type="text" id="agent" class="form-control" >
                    </div>
                </div>
                <div class="form-group row">
                    <label for="lendTime"  class="col-lg-2 col-form-label">借出时间：</label>
                    <div class="col-lg-10">
                        <input type="date" id="lendTime" value="<%=tDate%>" class="form-control" >
                    </div>
                </div>

                <div class="form-group row">
                    <div class="col-12 col-md-12">
                        <div class="row">
                            <div class="col-8 offset-6">
                                <button type="submit" onclick="sendData()" class="btn btn-success mr-5">借出设备</button>
                                <button type="button" onclick="setste()" class="btn btn-primary">重置</button>
                            </div>
                        </div>
                    </div>

                </div>
            </form>

        </div>

        <div class="col-12 col-md-2"></div>
    </div>

</div>



<%--接收后端中的model值待js取--%>
    <div id="eps" style="display: none">
        ${ep}
    </div>
</body>

<script type="text/javascript">
    var notNull = new RegExp(".*[^\s]","i");
    var notNumber=new RegExp("^[1-9]\d*$");
    var $trs1=document.getElementById("myForm").getElementsByClassName("form-group");
    var prefixPath="${pageContext.request.contextPath}";
    $(function () {
        customTips//警告框居中显示
        //判读该设备是否存在
        $("#eName").on("blur",function () {
            var ll=$("#eName").val();
            if (notNull.test(ll)){
                subEp($trs1[0],"eName","该设备不存在");
            }else {
                StartCheck($trs1[0],"eName","不可为空","en",false);
            }
        });

        //判读该设备是否有该类型
        $("#etype").on("change",function () {
            console.log("sss")
            var ll=$("#etype").val();
            if (notNull.test(ll)){
                subEp($trs1[1],"etype","该设备没有这个类型");
            }else {
                StartCheck($trs1[1],"etype","不可为空","et",false);
            }
        });

        //判读申请人
        $("#lendName").on("blur",function () {
            var ll=$("#lendName").val();
            if (notNull.test(ll)){
                StartCheck($trs1[2],"lendName","","sd",true);
            }else {
                StartCheck($trs1[2],"lendName","不可为空","sd",false);
            }
        });

        //借用数量
        $("#number").on("blur",function () {
            var ll=$("#number").val();
            if (!notNull.test(ll)){
                StartCheck($trs1[4],"number","不可为空","nu",false);
            }else if(!notNumber.test(ll)){
                StartCheck($trs1[4],"number","输入的不是数字","nu",false);
            }else{
                StartCheck($trs1[4],"number","","nu",true);
            }
            console.log("释放大量")
        });


        //
        $("#agent").on("blur",function () {
            var ll=$("#agent").val();
            if (!notNull.test(ll)){
                StartCheck($trs1[5],"agent","不可为空","ce",false);
            }else{
                StartCheck($trs1[5],"agent","不可为空","ce",true);
            }
        });

    });



    //设备对象
    function Equipment(eId,eName,etype,brand,model,mf,num,pTime,dTime) {
          this.eId=eId;
          this.eName=eName;
          this.etype=etype;
          this.brand=brand;
          this.model=model;
          this.manufacturer=mf;
          this.num=num;
          this.purchaseTime=pTime;
          this.deliveryTime=dTime;
    }
    //借还对象
    function RL(eId,ggent,borrower,number,department,epId,lendTime,reTime,jobNumber,state) {
        this.eId=eId;
        this.ggent=ggent;
        this.borrower=borrower;
        this.lendNum=number;
        this.department=department;//部门
        this.epId=epId;
        this.lendTime=lendTime;
        this.reTime=reTime;
        this.jobNumber=jobNumber;
        this.state=state;
    }
    //类型对象
    function EType(id,type) {
        this.id=id;
        this.type=type;
    }
    //部门设备
    function Department(id,lendDepar) {
        this.id=id;
        this.lendDepar=lendDepar;
    }

    //<![CDATA[

    var flag1=false;
    var eId=0;//得到的设备id
    function subEp($element,Ipid,val1) {
            var tid=$("#etype").val();
            var eName=$("#eName").val();
            var model= $("#model").val();

            var etype=new EType(tid,null);
            var ep=new Equipment(0,eName,etype,null,model,null,0,null,null);

            if (notNull.test(eName)&&notNull.test(model)) flag1=true;

            $.ajax({
                async:false,//开启同步
                type:"post",
                contentType:"application/json;charset=utf-8",
                dataType:"json",
                url:prefixPath+"/lr/queryEp",
                data:JSON.stringify({"eName":eName,"typeId":tid}),
                success:function (data) {
                    if (!data.state){
                        StartCheck($element,Ipid,val1,"en",false);
                    }else{
                        eId=data.eId;
                        StartCheck($element,Ipid,val1,"et",true);
                        StartCheck($element,"eName",val1,"en",true);
                    }
                }
            });
    }

    function  sendData() {
        var agen= $("#agent").val();//处理人
        var number= $("#number").val();//数量
        var lendName= $("#lendName").val();//量借用人
        var lendTime= $("#lendTime").val();//借出时间
        var dpId=$("#dp").val();
        var eName= $("#eName").val();//设备名
        var model=$("#model").val();//型号
        var etpId=$("#etype").val();//类型id
        var etype=$("#etype").text();//类型
        var jo= $("#jobNumber").val();//工号

        var etypeC=new EType(parseInt(etpId),"");
        var eq= new Equipment();
        eq.eId=eId;
        eq.model=model;
        eq.etype=etypeC;
        eq.eName=eName;

        var department=new Department(parseInt(dpId),"");


        var rl= new RL(0,agen,lendName,parseInt(number),department,eq,lendTime,null,jo,"已借出");

        if (agen!=null&&agen.trim()!=""&&number!=null&&number.trim()!=""&&
            lendName!=null&&lendName.trim()!=""&&
            lendTime!=null&&lendTime.trim()!=""){
            $.ajax({
                type:"post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                url: prefixPath+"/lr/addLend",
                data:JSON.stringify(rl),
                success:function (data) {
                    if (data.state){
                        toastr.success("登记成功");
                        setste();
                    }
                },
                error:function (e) {
                    toastr.error("服务器异常");
                    console.log(e);
                }
            });


        }else {
            toastr.warning("请填写数据");
        }

        return false;
    }


    function setste() {
         window.parent.frames.detailed("/pj/addLendPage");
    }

    // ]]>

</script>

</html>